<?php
/**
 * 菜单Model类
 * @file 	MenuModel.class.php
 * @author 	kelecode@gmail.com
 * @date 	2013-06-13
 * modify       
 */
class MenuModel extends Model{
	
	private $table;
	
	public function __construct(){
		parent::__construct();
		
		$this->table = C('db_table_prefix').'menu';
	}
	
	/**
	 * 添加菜单
	 * @param array $data
	 * @return boolean
	 */
	public function addMenu($data){
		foreach($data as $key => $val){
			$val = trim($val);
			$data[$key] = addslashes($val);
		}
		
		$menu_type = $data['menu_type'];
		$name = $data['name'];
		$alias = $data['alias'];
		$link = $data['link'];
		$link_type = $data['link_type'];
		$parent_id = $data['parent_id'];
		$component_id = $data['component_id'];
		$sort = $data['sort'];
		$params = $data['params'];
		$ctime = $utime = time();
		$status = $data['status'];
		
		$sql = "INSERT INTO `{$this->table}`
			(`id`,`menu_type`,`name`,`alias`,`link`,`link_type`,`parent_id`,`component_id`,`sort`,`params`,`ctime`,`utime`,`status`) VALUES 
			(NULL,'{$menu_type}','{$title}','{$alias}','{$link}','{$link_type}','{$parent_id}','{$component_id}','{$sort}','{$params}','{$ctime}','{$utime}','{$status}')";
		$result = $this->execute($sql);
		
		return $result;
	}
	
	/**
	 * 获取获取全部或单个菜单
	 * @param 	int $id 
	 * @return 	array $menuList
	 */
	public function menuList($id=null){
		$menuList = array();
		$sql = "SELECT * FROM `{$this->table}` WHERE `id`>0";
		if($id){
			$sql .= " AND `id`='{$id}'";
		}
		$sql .= " ORDER BY `sort` ASC,`id` ASC";
		$menuList = $this->query($sql);
		
		return $menuList;
	}
	
	/**
	 * 更新状态
	 */
	public function updateStatus($ids, $status){
		if(!is_array($ids) || empty($ids)){
			return false;
		}
		$ids = implode(',',$ids);
		$status = !in_array($status,array(0,1)) ? 1 : $status;
		$utime = time();
		$sql = "UPDATE `{$this->table}` SET `status`='{$status}', `utime`='{$utime}' WHERE `id` in ({$ids})";
		$result = $this->execute($sql);

		return $result;
	}

	/**
	 * 更新排序
	 */
	public function updateSort($id, $num){
		if( empty($id) ){
			return false;
		}
		$utime = time();
		$sql = "UPDATE `{$this->table}` SET `sort`='{$num}', `utime`='{$utime}' WHERE `id`='{$id}' LIMIT 1";
		$result = $this->execute($sql);
		
		return $result;
	}
	
	/**
	 * 获取单条菜单信息
	 */
	public function getMenuById($id){
		if(empty($id)){
			return false;
		}
		$category = $this->menuList($id);
		
		return $category[0];
	}	
	
	/**
	 * 编辑菜单
	 * @return boolean true or false
	 */
	public function editMenu($data){
		if(empty($data['id'])){
			return false;
		}
		$utime = time();
		$sql = "UPDATE `{$this->table}` SET 
					`menu_type`='{$data['menu_type']}', `name`='{$data['name']}', `alias`='{$data['alias']}', `link`='{$data['link']}', `link_type`='{$data['lnk_type']}', `parent_id`='{$data['parent_id']}', `component_id`='{$data['component_id']}', `sort`='{$data['sort']}', `params`='{$data['params']}', `utime`='{$utime}', `status`='{$data['status']}' WHERE `id`='{$data['id']}' LIMIT 1";
		$result = $this->execute($sql);
		
		return $result;
	}
	
	/**
	 * 删除菜单
	 * @param int $id
	 * @return boolean true or false
	 */
	public function delMenu($id){
		if(empty($id)){
			return false;
		}
		$sql = "UPDATE `{$this->table}` SET `status`='0' WHERE `id`='{$id}' LIMIT 1";
		$result = $this->execute($sql);
		
		return $result;
	}	

}